package com.yswee.mallnavi.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.yswee.mallnavi.activity.base.BaseActivity;
import com.yswee.framework.utils.OSUtils;
import com.yswee.mallnavi.Preference;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.activity.common.SiteActivity;
import com.yswee.mallnavi.context.ContextConstant;

import java.util.ArrayList;

public class SplashActivity extends BaseActivity {
	
	private static final int MSGWHAT_GOTONEXT = 0;
	private static final int MSGDELAY_GOTONEXT = 0;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSGWHAT_GOTONEXT:
				if (Preference.getCompany() == null) {
					startActivity(SiteActivity.class);
				} else {
					startActivity(MainActivity.class);
				}
				finish();
				break;
			}
		};
	};
	
	@Override
	protected boolean isFullScreen() {
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		OSUtils.hideNavigationBar(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		gotoNextPage();
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	protected void onApplyData() {
		super.onApplyData();
	}
	
	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if (requestCode == ContextConstant.REQUESTCODE_PERMISSION) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                	
                }
            }
            gotoNextPage();
        }
	}
	
	private boolean requestPermission() {
		String[] permissions = new String[] {
				Manifest.permission.WRITE_EXTERNAL_STORAGE,
				Manifest.permission.READ_EXTERNAL_STORAGE,
				Manifest.permission.READ_PHONE_STATE
		};
		ArrayList<String> array = new ArrayList<String>();
		for (String permission : permissions) {
	        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
	            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
	            	array.add(permission);
	            } else {
	            	array.add(permission);
	            }
	        }
		}
		if (array.size() > 0) {
			String[] needs = new String[array.size()];
			array.toArray(needs);
			ActivityCompat.requestPermissions(this, needs, ContextConstant.REQUESTCODE_PERMISSION);
			return false;
		} else {
			return true;
		}
    }
	
	private void gotoNextPage() {
		if (requestPermission()) {
			mHandler.removeMessages(MSGWHAT_GOTONEXT);
			mHandler.sendEmptyMessageDelayed(MSGWHAT_GOTONEXT, MSGDELAY_GOTONEXT);
		}
	}
	
}